<?php
namespace app\__MODULE__\controller;
use app\__MODULE__\model\StoreCategory as StoreCategoryModel;

class StoreCategory extends Base {

    /**
     * 首页
     */
    public function index(){
        $search  = request()->param();
        $where = [];
        if(isset($search['pid'])&&$search['pid'] !=''){
            $where[] = ['pid','=',$search['pid']];
        }else{
            $where[] = ['pid','=',0];
        } 
        if(IS_POST) {
            $db   = StoreCategoryModel::where($where)->order('id desc');
            $list = $db->limit(paginator())->select();
            if(count($list)){
                foreach($list as $item){
                    if ($item['pid']) {
                        $item['pid_name'] = StoreCategoryModel::where('id', $item['pid'])->value('cate_name');
                    } else {
                        $item['pid_name'] = '顶级';
                    }
                }
            }
            __MODULE___response(0, 'success', $list, $db->count());
        }

        return $this->fetch();
    }


   /**
    * 显示切换
    */
    public function is_show(){
        $id = param_check('id');
        $data = StoreCategoryModel::find($id);
        $data->is_show = request()->param('checked');
        if($data->save()) json_response(1,'success');
        json_response(0,'error');
    }

    /**
     * 添加商品分类
     */
    public function add(){
        if(request()->isPost()) {
            $data = request()->post();
            $data['add_time']  = time();
            if(StoreCategoryModel::create($data)) json_response(1,'操作成功');
            json_response(0,'操作失败');
         }else{
            $option = StoreCategoryModel::where('pid',0)->field('id as value,cate_name as title')->order('sort desc')->select();
            return $this->render_form([
                ['type'=>'select', 'name'=>'pid', 'title'=>'父级','option'=>$option,'lay-search'=>'true'],
                ['type'=>'text', 'name'=>'cate_name', 'title'=>'分类名称', 'lay-verify'=>'required'],
                ['type'=>'upload','name'=>'pic', 'title'=>'分类图片'],
                ['type'=>'text','text_type'=>'number','name'=>'sort', 'title'=>'排序'],
                ['type'=>'radio','name'=>'is_show', 'title'=>'是否显示','option'=>[
                    ['title'=>'显示','value'=>1],
                    ['title'=>'隐藏','value'=>0]
                ]],
            ]);
         }
    }

    /**
     * 编辑分类
     */
    public function edit(){
        $id = \param_check('id');
        if(request()->isPost()) {
            $data = request()->post();
            if(StoreCategoryModel::update($data,['id'=>$id])) json_response(1,'操作成功');
            json_response(0,'操作失败');
         }else{
            $data   = StoreCategoryModel::find($id);
            $option = StoreCategoryModel::where('pid',0)->field('id as value,cate_name as title')->order('sort desc')->select();
            return $this->render_form([
                ['type'=>'select', 'name'=>'pid', 'title'=>'父级','option'=>$option,'lay-search'=>'true'],
                ['type'=>'text', 'name'=>'cate_name', 'title'=>'分类名称', 'lay-verify'=>'required'],
                ['type'=>'upload', 'name'=>'pic', 'title'=>'分类图片'],
                ['type'=>'text','text_type'=>'number','name'=>'sort', 'title'=>'排序'],
                ['type'=>'radio','name'=>'is_show', 'title'=>'是否显示','option'=>[
                    ['title'=>'显示','value'=>1],
                    ['title'=>'隐藏','value'=>0]
                ]],
            ],$data);
         }
    }

    /**
     * 删除分类
     * @return [type] [description]
     */
    public function del(){
        $id = param_check('id');
        if(!$category = StoreCategoryModel::get($id)) json_response(0,'无此分类');
        if(StoreCategoryModel::where('pid',$id)->count()) json_response(0,'该分类下有子分类，不能直接删除');
        if($category->delete()) json_response(1,'删除成功');
        json_response(0,'删除失败');
    }

}